<template>
  <div class="app-container">
    <h2 style="text-align: center">发布新课程</h2>
    <el-steps
      :active="2"
      process-status="wait"
      align-center
      style="marginbottom: 40px"
    >
      <el-step title="填写课程基本信息" />
      <el-step title="创建课程大纲" />
      <el-step title="最终发布" />
    </el-steps>
    <el-button type="text" @click="openSaveChapter()">添加章节</el-button>
    <!-- 添加和修改章节表单 -->
    <el-dialog :visible.sync="dialogChapterFormVisible" title="添加章节">
      <el-form :model="chapter" label-width="120px">
        <el-form-item label="章节标题">
          <el-input v-model="chapter.title" />
        </el-form-item>
        <el-form-item label="章节排序">
          <el-input-number
            v-model="chapter.sort"
            :min="0"
            controlsposition="right"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogChapterFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveOrUpdate">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 章节 -->
    <ul class="chanpterList">
      <li v-for="chapter in chapterVideoList" :key="chapter.id">
        <p>
          {{ chapter.title }}
          <span class="acts">
            <el-button type="text" @click="openSaveVideo(chapter.id)"
              >添加课时</el-button
            >
            <el-button
              style=""
              type="text"
              @click="openUptateChapter(chapter.id)"
              >编辑</el-button
            >
            <el-button type="text" @click="deleteEntityById(chapter.id)"
              >删除</el-button
            >
          </span>
        </p>
        <!-- 视频 -->
        <ul class="chanpterList videoList">
          <li v-for="video in chapter.children" :key="video.id">
            <p>
              {{ video.title }}
              <span class="acts">
                <el-button type="text" @click="openUpdateVideo(video.id)"
                  >编辑</el-button
                >
                <el-button type="text" @click="deleteVideoById(video.id)"
                  >删除</el-button
                >
              </span>
            </p>
          </li>
        </ul>
      </li>
    </ul>
    <el-form label-width="120px">
      <el-form-item>
        <el-button @click="previous">上一步</el-button>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="next"
          >下 一步</el-button
        >
      </el-form-item>
    </el-form>

    <template>
      <!-- 添加和修改课时表单 -->
      <el-dialog :visible.sync="dialogVideoFormVisible" title="添加课时">
        <el-form :model="video" label-width="120px">
          <el-form-item label="课时标题">
            <el-input v-model="video.title" />
          </el-form-item>
          <el-form-item label="课时排序">
            <el-input-number
              v-model="video.sort"
              :min="0"
              controlsposition="right"
            />
          </el-form-item>
          <el-form-item label="是否免费">
            <el-radio-group v-model="video.isFree">
              <el-radio :label="true" :value="true">免费</el-radio>
              <el-radio :label="false" :value="false">收费</el-radio>
            </el-radio-group>
          </el-form-item>

          <!-- TODO -->
          <el-form-item label="上传视频">
            <el-upload
              :on-success="handleVodUploadSuccess"
              :on-remove="handleVodRemove"
              :before-remove="beforeRemove"
              :on-exceed="handleUploadExceed"
              :file-list="fileList"
              :action="BASE_API + '/api/vod/uploadAlyVideo'"
              :limit="1"
              class="upload-demo"
            >
              <el-button size="small" type="primary">上传视频</el-button>
              <el-tooltip placement="right-end">
                <div slot="content">
                  最大支持1G，<br />
                  支持3GP、ASF、AVI、DAT、DV、FLV、F4V、<br />
                  GIF、M2T、M4V、MJ2、MJPEG、MKV、MOV、MP4、<br />
                  MPE、MPG、MPEG、MTS、OGG、QT、RM、RMVB、<br />
                  SWF、TS、VOB、WMV、WEBM 等视频格式上传
                </div>
                <i class="el-icon-question" />
              </el-tooltip>
            </el-upload>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogVideoFormVisible = false">取 消</el-button>
          <el-button
            :disabled="saveVideoBtnDisabled"
            type="primary"
            @click="saveOrUpdateVideo"
            >确 定</el-button
          >
        </div>
      </el-dialog>
    </template>
  </div>
</template>
    <script>
import chapterApi from "@/api/edu/chapter";
import videoApi from "@/api/edu/video";
import basePostData from "@/api/edu/basePostData";
export default {
  data() {
    return {
      BASE_API: process.env.BASE_API, // 接口API地址
      // 添加表单
      dialogChapterFormVisible: false,
      // 树形列表
      chapterVideoList: [],
      courseId: "",
      saveBtnDisabled: false, // 保存按钮是否禁用
      // 封装章节数据
      chapter: {
        id: "",
        title: "",
        sort: "",
        //课程id
        courseId: "",
      },
      // =================================================小节操作+++++++++++++++++++++++++++++++++++++++
      // 小结
      video: {
        id: "",
        isFree: "",
        courseId: "",
        chapterId: "",
        videoSourceId: "",
        videoOriginalName: "",
      },
      dialogVideoFormVisible: false,
      saveVideoBtnDisabled: false,

      fileList: [],
    };
  },
  created() {
    console.log("chapter created");
    if (this.$route.params && this.$route.params.id) {
      this.chapter.courseId = this.$route.params.id;
      this.courseId = this.$route.params.id;
    }
    // 根据 课程id获取其list
    this.getCharterVideo();
  },
  methods: {
    // 添加弹窗打开
    openSaveChapter() {
      this.dialogChapterFormVisible = true;
      // 表单数据清空
      this.chapter = {};
      // 保存路由id不然报错
      this.chapter.courseId = this.courseId;
    },
    // 修改
    openUptateChapter(id) {
      // 数据回写
      this.getChapterInfo(id);
      this.dialogChapterFormVisible = true;
    },
    // 上一步
    previous() {
      console.log("previous");
      this.$router.push({ path: "/course/info/" + this.chapter.courseId });
    },
    next() {
      console.log("next");
      this.$router.push({ path: "/course/publish/" + this.chapter.courseId });
    },
    // 获取数据列表
    getCharterVideo() {
      chapterApi.getCharterVideo(this.chapter.courseId).then((res) => {
        this.chapterVideoList = res.data.info;
      });
    },
    // 根据id获取章节信息
    getChapterInfo(id) {
      chapterApi.getChapterInfo(id).then((res) => {
        this.chapter = res.data.info;
        console.log("根据id获取章节信息:", this.chapter);
      });
    },
    deleteEntityById(id) {
      this.$confirm("此操作将永久删除章节记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //点击确定，执行then方法
          //调用删除的方法
          chapterApi.deleteEntityById(id).then((res) => {
            console.log("deleteEntityById:", res);
            if (res.code == 20000) {
              //删除成功
              //提示信息
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              // 刷新页面数据
              this.getCharterVideo();
            } else {
              //删除成功
              //提示信息
              this.$message({
                type: "error",
                message: res.message,
              });
            }
          });
        })
        //点击取消，执行catch方法
        .catch((error) => {
          this.$message("取消删除");
        });
    },
    updateChapter() {
      chapterApi.updateEntity(this.chapter).then((res) => {
        // 关闭弹窗
        this.dialogChapterFormVisible = false;
        // 提示信息
        this.$message({
          type: "success",
          message: "修改成功",
        });
        // 刷新页面数据
        this.getCharterVideo();
      });
    },
    saveChapter() {
      chapterApi.saveEntity(this.chapter).then((res) => {
        // 关闭弹窗
        this.dialogChapterFormVisible = false;
        // 提示信息
        this.$message({
          type: "success",
          message: "添加成功",
        });
        // 刷新页面数据
        this.getCharterVideo();
        this.chapter = {};
        // 保存路由id不然报错
        this.chapter.courseId = this.courseId;
      });
    },
    saveOrUpdate() {
      this.dialogChapterFormVisible = true;
      if (this.chapter.id) {
        this.updateChapter();
      } else {
        this.saveChapter();
      }
    },
    // =================================================小节操作+++++++++++++++++++++++++++++++++++++++
    // 添加小节
    openSaveVideo(chapterId) {
      this.video = {};
      // 设置章节id
      this.video.chapterId = chapterId;
      this.dialogVideoFormVisible = true;
    },
    // 编辑小节的内容
    openUpdateVideo(videoId) {
      this.getVideoInfo(videoId);
      // 打开弹窗
      this.dialogVideoFormVisible = true;
    },
    deleteVideoById(id) {
      this.$confirm("此操作将永久删除章节记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        //点击确定，执行then方法
        //调用删除的方法
        videoApi.deleteEntityById(id).then((res) => {
          console.log("deleteEntityById:", res);
          if (res.code == 20000) {
            //删除成功
            //提示信息
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            // 刷新页面数据
            this.getCharterVideo();
          } else {
            //删除成功
            //提示信息
            this.$message({
              type: "error",
              message: res.message,
            });
          }
        });
      });
    },
    updateVideo() {
      videoApi.updateEntity(this.video).then((res) => {
        // 关闭弹窗
        this.dialogVideoFormVisible = false;
        // 提示信息
        this.$message({
          type: "success",
          message: "修改成功",
        });
        // 刷新页面数据
        this.getCharterVideo();
      });
    },
    saveVideo() {
      // 课程id
      this.video.courseId = this.courseId;
      // console.log(this.video)
      videoApi.saveEntity(this.video).then((res) => {
        // 关闭弹窗
        this.dialogVideoFormVisible = false;
        // 提示信息
        this.$message({
          type: "success",
          message: "添加成功",
        });
        // 刷新页面数据
        this.getCharterVideo();
        this.video = {};
        this.fileList = [];
        // 保存路由id不然报错
        // this.chapter.courseId = this.courseId;
      });
    },
    saveOrUpdateVideo() {
      if (this.video.id) {
        this.updateVideo();
      } else {
        this.saveVideo();
      }
    },
    getVideoInfo(id) {
      videoApi.getVideoInfo(id).then((res) => {
        this.video = res.data.info;
        console.log("根据id获取章节信息:", this.video);
      });
    },
    // =================================视屏视屏
    //成功回调
    handleVodUploadSuccess(response, file, fileList) {
      this.video.videoSourceId = response.data.info;
      this.video.videoOriginalName = file.name;
      console.log("handleVodUploadSuccess#file:", file, fileList);
    },
    //视图上传多于一个视频
    handleUploadExceed(files, fileList) {
      this.$message.warning("想要重新上传视频，请先删除已上传的视频");
    },
    // 文件移除
    handleVodRemove(file, fileList) {
      basePostData
        .deleteEntityById(
          "/api/vod",
          "/delAlyVideoById",
          this.video.videoSourceId
        )
        .then((res) => {
          console.log("移除上传的文件", res);
          this.$message({
            type: "success",
            message: "移除视屏成功",
          });
          this.fileList = [];
          this.video.videoSourceId = "";
          this.video.videoOriginalName = "";
        });
    },
    // 文件移除之前
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },
  },
};
</script>
<style scoped>
.chanpterList {
  position: relative;
  list-style: none;
  margin: 0;
  padding: 0;
}
.chanpterList li {
  position: relative;
}
.chanpterList p {
  /* float: left; */
  font-size: 20px;
  margin: 10px 0;
  padding: 10px;
  height: 70px;
  line-height: 50px;
  width: 100%;
  border: 1px solid #ddd;
}
.chanpterList .acts {
  float: right;
  font-size: 14px;
}
.videoList {
  padding-left: 50px;
}
.videoList p {
  /* float: left; */
  font-size: 14px;
  margin: 10px 0;
  padding: 10px;
  height: 50px;
  line-height: 30px;
  width: 100%;
  border: 1px dotted #ddd;
}
</style>